﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using FTchina;

public partial class UI_fenlei : System.Web.UI.Page
{
     string action = "";//list:列表 del:删除 mod:修改
    protected void Page_Load(object sender, EventArgs e)
     {
         basic.CheckAdmin(this);
         basic.ChkPoewr(this, FTchina.Api.htmlpath.GetCurrentPagePath());
         action = basic.FilterSQL(Request["action"]);
        if (action.Length == 0) action = "list";
        if (!IsPostBack)
        {
            LoadList();
            switch (action)
            {
                case "del": Delete(); break;
                case "mod": modify(); break;
            }
            plrootadd.Visible = CheckRoot();//自动判断是否显示添加根分类操作区域
        }
    }

    private void modify()
    {
        plmodi.Visible = true;
      
        txbyname.Text =shujuku.GetDataTable("select * from lanmu where id="+basic.FilterSQL(Request["id"])).Rows[0]["context"].ToString();
        txbpx.Text = shujuku.GetDataTable("select * from lanmu where id=" + basic.FilterSQL(Request["id"])).Rows[0]["paixu"].ToString();
        txblink.Text = shujuku.GetDataTable("select * from lanmu where id=" + basic.FilterSQL(Request["id"])).Rows[0]["link"].ToString();
        if (shujuku.GetDataTable("select * from lanmu where id=" + basic.FilterSQL(Request["id"])).Rows[0]["pinglun"].ToString() == "1") chkpl.Checked = true;
        ddltg.SelectedValue = shujuku.GetDataTable("select * from lanmu where id=" + basic.FilterSQL(Request["id"])).Rows[0]["tougao"].ToString();
        
    }

    private void Delete()
    {
        string id = basic.FilterSQL(Request["id"]);
        if (id.Length == 0) { JS.Alert("不允许的操作！"); return; }

        //获取ID值
        GetAllId(id, 0);
        //删除分类条目
        DeleteFenleiID();

    }


    #region 加载分类


     
    /// <summary>
    /// 加载整个分类表
    /// </summary>
    private void LoadList()
    {
        
        litlist.Text = "";
        ddllm.Items.Clear();
        ddlold.Items.Clear();
        ddlnew.Items.Clear();
        int MTcount = 0;//执行遍历的次数
        //开始迭代加载
        MakeTr("0", MTcount);

    }
    /// <summary>
    /// 装配每一行
    /// </summary>
    /// <param name="id">上级ID</param>
    /// <param name="count">迭代次数</param>
    private void MakeTr(string id, int count)
    {

        DataView dv = new DataView(shujuku.GetDataTable("select * from lanmu where parentid=" + id + " order by paixu desc,id desc"));
        foreach (DataRowView drv in dv)
        {
            //形成表格
            litlist.Text += "<tr><td>" + MakeFelgefu(count) + drv["context"].ToString() + "</td><td>" + drv["paixu"].ToString() + "," + drv["pinglun"].ToString() + "," + drv["tougao"].ToString() + "</td><td>" + drv["link"].ToString() + "</td><td> <a href=del.aspx?T=lanmu&id=" + drv["id"].ToString() + "><a href='?action=mod&id=" + drv["id"] + "'  title='header=[说明] body=[对分类名称、排序序号、是否允许评论等进行修改]'>修改</a> <a href='?action=del&id=" + drv["id"] + "' title='header=[删除说明] body=[被删除项如果包含子项，那么所有的子项也将被删除]'>删除</a></td></tr>";

            //形成下拉列表
            ListItem li = new ListItem(MakeFelgefu(count) + drv["context"].ToString(), drv["id"].ToString());
            ddllm.Items.Add(li);
            ddlnew.Items.Add(li);
            ddlold.Items.Add(li);
            //再次遍历
            MakeTr(drv["id"].ToString(), count + 1);

        }

    }
    /// <summary>
    /// Make分隔符
    /// </summary>
    /// <param name="count"></param>
    /// <returns></returns>
    private string MakeFelgefu(int count)
    {
        string Returnwords = string.Empty;
        if (count == 0)
        {
            Returnwords = "";
        }

        else
        {
            Returnwords = ("├").PadLeft(count, '　');
        }
        return Returnwords;
    }

    #endregion

    #region 删除分类

    ArrayList ayl = new ArrayList();
    /// <summary>
    /// 获取系统要删除的对象的ID列表
    /// </summary>
    /// <param name="id"></param>
    private void GetAllId(string id, int count)
    {
        if (count == 0) { ayl.Add(id); }
        DataView dv = new DataView(shujuku.GetDataTable("select * from lanmu where parentid=" + id + " order by id asc"));
        foreach (DataRowView drv in dv)
        {
            ayl.Add(drv["id"].ToString());

            GetAllId(drv["id"].ToString(), count + 1);

        }

    }
    /// <summary>
    /// 将ArrayList中的值转换为sql中的值
    /// </summary>
    /// <param name="aly"></param>
    /// <returns></returns>
    private string ArrayList2String(ArrayList aly)
    {
        string ReturnWords = "";
        foreach (string item in aly)
        {
            ReturnWords += item + ",";
        }
        ReturnWords = ReturnWords.Substring(0, ReturnWords.Length - 1);
        return ReturnWords;
    }
    /// <summary>
    /// 具体的执行删除的语句
    /// </summary>
    private void DeleteFenleiID()
    {
        string sql = "delete from lanmu where id in (" + ArrayList2String(ayl) + ")";
        shujuku.ExecuteSql(sql);
        LoadList();
    }
    #endregion


    #region 按钮行为
    protected void btnziok_Click(object sender, EventArgs e)
    {
        string parentid = ddllm.SelectedValue.ToString();
        string context = basic.FilterSQL(txbzifl.Text);
        string sql = "insert into lanmu (parentid,context) values(" + parentid + ",'" + context + "') ";
        UImsg.PageRender(shujuku.ExecuteSql(sql));

    }
    protected void btnroot_Click(object sender, EventArgs e)
    {
        string context = basic.FilterSQL(txbroot.Text);
        string sql = "insert into lanmu (parentid,context) values('0','" + context + "') ";
        UImsg.PageRender(shujuku.ExecuteSql(sql));

    }
    protected void btnmodok_Click(object sender, EventArgs e)
    {
        string context = basic.FilterSQL(txbyname.Text);
        string id = basic.FilterSQL(Request["id"]);
        string paixu = basic.FilterSQL(txbpx.Text);
        string link = basic.SafeRequest(txblink.Text);
        string tougao = ddltg.SelectedValue;
         
        string pinglun = (this.chkpl.Checked) ? "1" : "0";
        if (id.Length == 0 || context.Length == 0) { JS.Alert("请输入完整！"); return; }
        string sql = "update lanmu set tougao='" + tougao + "' ,pinglun='" + pinglun + "',link='" + link + "',paixu=" + paixu + ",context='" + context + "' where id=" + id;
        UImsg.PageRender(shujuku.ExecuteSql(sql));

    }
    #endregion
    protected void btnmove_Click(object sender, EventArgs e)
    {
        string old = ddlold.SelectedValue;
        string newfenle = ddlnew.SelectedValue;
        string sql = "update lanmu set parentid=" + newfenle + " where id=" + old;
        UImsg.PageRender(shujuku.ExecuteSql(sql));
    }


    protected Boolean CheckRoot()
    {
        return !shujuku.testread("select id from lanmu where parentid=0");
    }
}